<?php

class WxPayNotifyCallBack extends WxPayNotify {

    //查询订单
    public function Queryorder($transaction_id) {

        $input = new \WxPayOrderQuery();
        $input->SetTransaction_id($transaction_id);
        $result = \WxPayApi::orderQuery($input);

        if(array_key_exists("return_code", $result)
            && array_key_exists("result_code", $result)
            && $result["return_code"] == "SUCCESS"
            && $result["result_code"] == "SUCCESS") {
            return true;
        }
        return false;

    }

    //重写回调处理函数
    public function NotifyProcess($data, &$msg) {
        \C\lib\Db::connect();

        if(!array_key_exists("transaction_id", $data)){
            $msg = "输入参数不正确";
            return false;
        }
        // 查询微信订单，判断订单真实性
        if(!$this->Queryorder($data["transaction_id"])){
            $msg = "订单查询失败";
            return false;
        }
        $feeback=new \C\mod\feedback();
        $feeback->set([
            'context'=>$data['out_trade_no'].'attach:'.$data['attach'].'money'.$data['total_fee'],
            'time_add'=>time()
        ]);
        $feeback->create();//exit;
//        $order=\C\mod\order::get(['order_pid'=>$data['out_trade_no']]);

        if(!empty($data['total_fee'])) {

//            $orders=\C\mod\order::getList(['order_pid'=>$data['out_trade_no']]);
//            foreach ($orders as $value){
//                $order= \C\mod\order::get(['id' => $value->id]);
//                $order->set([
//                    'pay_type'=>1
//                ]);
//                $order->update();
//            }
            $feeback=new \C\mod\feedback();
            $feeback->set([
                'context'=>$data['total_fee'],

            ]);
            $feeback->create();
            $order=\C\mod\order::get(['order_pid'=>$data['out_trade_no']]);

            $order->set([
                'pay_type'=>1
            ]);
            $order->update();
            //修改用户资产
            $wprice=\C\mod\wprice::get(['worker_id'=>$order->worker_id]);
            $proute=new \C\mod\proute();
            if($order->type==0){
                $money=$order->price-1;
            }else{
                $money=$order->price*0.8;
            }



            $wprice->set([
                'price'=>$wprice->price+$money
            ]);
            $proute->set([
                'worker_id' => $order->worker_id,
                'time_add' => $_SERVER['REQUEST_TIME'],
                'price' => $money,
                'order_id' => $data['out_trade_no'],
                'pay_type' => 'wxg',
                'pay_state' => 1,
                'remark' => 3
            ]);

            $wprice->update();
            $proute->create();


            //送优惠券0送代取券 1送邮寄券
            $voucher=new \C\mod\voucher();
            if($order->type==0){
                $voucher->set([
                    'user_id'=> $order->user_id,
                    'type'=>1,//2寄快递 1取快递
                    'discount'=>1,
                    'state'=>0,
                    'time_add'=>$_SERVER['REQUEST_TIME'],
                    'end_time'=>$_SERVER['REQUEST_TIME']+15*24*3600
                ]);
            }else{
                $voucher->set([
                    'user_id'=> $order->user_id,
                    'type'=>2,//2寄快递 1取快递
                    'discount'=>0.9,
                    'state'=>0,
                    'time_add'=>$_SERVER['REQUEST_TIME'],
                    'end_time'=>$_SERVER['REQUEST_TIME']+15*24*3600
                ]);
            }

            $voucher->create();

            $user=\C\mod\user::get(['id'=>$order->user_id]);
            //获取快递员名字
            $worderd=\C\mod\workerd::get(['worker_id'=>$order->worker_id]);

            //付款成功通知
            \C\lib\Wxapi::sendInfo($user->wx_openid,'',$order->order_id,3,$worderd->realname,$worderd->phone,'','','');



        } else {
            $msg = '订单不在数据库';
            return false;
        }
        if($order = \C\mod\order::get(['id' => 1])) {
            $order->set([
                'order_id' => time()
            ]);
            $order->update();
        } else {
            $msg = '订单不在数据库';
            return false;
        }
        return true;

    }

}

